/**
 * CaddieApplication.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.model;

import java.util.*;

/**
 * @author windu.2b
 * 
 */
public abstract class CaddieApplication
{
	private List<Caddie>	     caddies	        = new ArrayList<Caddie>();


	private List<CaddieListener>	caddieListeners	= new ArrayList<CaddieListener>();




	public void addCaddieListener( CaddieListener listener )
	{
		caddieListeners.add( listener );
	}




	public void removeCaddieListener( CaddieListener listener )
	{
		caddieListeners.remove( listener );
	}




	public List<Caddie> getCaddies()
	{
		return Collections.unmodifiableList( caddies );
	}




	public void addCaddie( Caddie caddie )
	{
		this.caddies = new ArrayList<Caddie>( this.caddies );
		caddies.add( caddie );
		fireCaddieEvent( caddie, CaddieEvent.Type.ADD );
	}




	public void deleteCaddie( Caddie caddie )
	{
		this.caddies = new ArrayList<Caddie>( this.caddies );
		caddies.remove( caddie );
		fireCaddieEvent( caddie, CaddieEvent.Type.DELETE );
	}




	private void fireCaddieEvent( Caddie caddie, CaddieEvent.Type eventType )
	{
		if ( !caddieListeners.isEmpty() )
		{
			CaddieEvent caddieEvent = new CaddieEvent( this, caddie, eventType );
			CaddieListener[] listeners = caddieListeners
			        .toArray( new CaddieListener[caddieListeners.size()] );

			for ( CaddieListener listener : listeners )
				listener.caddieChanged( caddieEvent );
		}
	}




	public abstract CaddieRecorder getCaddieRecorder();




	public abstract UserPreferences getUserPreferences();
}